#include<bits/stdc++.h>
#define rg register
#define file(x)freopen(x".in","r",stdin);freopen(x".out","w",stdout)

using namespace std;

const long long mod=998244353;
inline long long ksm(long long a,long long b)
{
	long long re=1;
	while(b)
	{
		if(b&1)re=re*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return re;
}

const int maxn=1e7+10;

int n,m,a,b,c;
int A[maxn];
inline int f(int x)
{
	if(x>=m)x-=m;
	int re=a;
	re=1ll*re*x%m;
	if((re+=b)>=m)re-=m;
	re=1ll*re*x%m;
	if((re+=c)>=m)re-=m;
	return re+1;
}

int Tong[maxn];
int B[maxn],top;

inline int gcd(int x,int y)
{
	if(!y)return x;
	return gcd(y,x%y);
}
struct Frac{
	int a,b;
	inline Frac(int _a=0,int _b=1)
	{
		int gc=gcd(_a,_b);
		a=_a,b=_b;
		if(gc)a/=gc,b/=gc;
	}
	inline void Fix()
	{
		int gc=gcd(a,b);
		if(!gc)return;
		a/=gc,b/=gc;
	}
}Zero(0,1),One(1,1);
inline bool operator ==(const Frac one,const Frac two)
{
	return one.a==two.a&&one.b==two.b;
}
inline bool operator < (const Frac one,const Frac two)
{
	return 1ll*one.a*two.b<1ll*two.a*one.b;
}
inline bool operator > (const Frac one,const Frac two)
{
	return 1ll*one.a*two.b>1ll*two.a*one.b;
}

inline void solve()
{
	scanf("%d",&n);scanf("%d",A+0);
	scanf("%d %d %d",&a,&b,&c);scanf("%d",&m);A[0]%=m;
	a%=m,b%=m,c%=m;
	for(rg int i=1;i<=n;i+=1)A[i]=f(A[i-1]);
	for(rg int i=1;i<=m;i+=1)Tong[i]=0;
	for(rg int i=1;i<=n;i+=1)++Tong[A[i]];
	top=0;
	for(rg int i=1;i<=m;i+=1)
		if(Tong[i])B[++top]=Tong[i]>=2;
	Frac Ans=One;
	for(rg int i=1;i<=top;i+=1)
	{
		Frac Min=One;
		auto calc=[&](int k,int b)
				  {
//					  cerr<<i<<" "<<k<<" "<<b<<endl;
					  if(k>=0)return;
					  else Min=min(Min,Frac(b,-k));
				  };
		int b=1,k=-B[i];
		calc(k,b);
		for(rg int j=i-1,flag=1;j;j-=1,flag^=1)
		{
			if(flag)
			{
				b+=1,k-=2;
				k-=B[j];
			}
			else
			{
				b-=1,k+=2;
				k+=B[j];
			}
			calc(k,b);
		}
		Ans=min(Ans,Min);
	}
	printf("%.5lf %lld\n",1.*Ans.a/Ans.b,Ans.a%mod*ksm(Ans.b%mod,mod-2)%mod);
}

int main()
{
//	file("guess3");
	int T;scanf("%d",&T);
	while(T--)solve();
	return 0;
}
